cd "L:/Project-Fire_Perimeter/Fire_perimeter/Processing/Ignitions/BufferSections/Sections/"

capture program drop mymargins
program mymargins

{
	syntax varlist, estname(string)
	local ll: display %9.0fc `e(ll)'	
	margins, dydx(`varlist') post
	est sto `estname'
	estadd local ll "`ll'"
}	
end

capture program drop data
program data

syntax namelist using, [maxleads(int 0) maxlags(int 0) recode BURNTmeasure(string) ///
	noDROPout tcuts(numlist) laglabel almon(numlist)] 
			
	noi {
		
	use `using', clear
		
	/************************************************************************************/
	/*Recode burnt variable. Code as zero all sections that are burnt after a section  	*/
	/*is unburnt. This guarantees for each fire direction will have a single burn 		*/
	/*duration, is an imperfect way of dealing with convex fires.						*/
		
	if "`burntmeasure'" == "centroid" {
		drop burnt
		rename burntalt burnt
		}
	else drop burntalt
		
	if "`recode'" != "recode" { /*Code as zero if beyond first extinguished cell*/
		sort firenum directionnum distnum
		capture drop count
		bysort firenum directionnum: gen count = sum(burnt)
		replace burnt = 0 if count < distnum & burnt == 1
		drop count
		}
	else if "`recode'" == "recode" { /*Code as 1 if not last extinguished cell*/
		sort firenum directionnum distnum
		capture drop count
		bysort firenum directionnum: gen count = sum(burnt)
		bysort firenum directionnum: egen max = max(count)
		replace burnt = 1 if count < max
		drop count max
		}
	
	sort firenum directionnum distnum	
	bysort firenum directionnum: egen x = sum(burnt)
	bysort  firenum directionnum: egen duration = max(x)
	drop x
		
	gen y = (distnum == duration + 1)
	
	/**********************************************************************************/
	/* Generate binned time-difference variables									  */		
		
	rename t toa

	local q = 0
	if "`tcuts'" != "" {
		foreach cut in `tcuts' {
			local q = `q' + 1
			if `q' == 1 gen t = `cut' if lnt <= `cut'
			else replace t = `cut' if lnt > `tmin' & lnt <= `cut'
			local tmin = `cut'
			}
		tab t, gen(t)
				
		local j = 0
		foreach var of varlist t1-t`q' {
			replace `var' = 0 if tmissing == 1
			local j = `j' + 1
			if `j' == 1 {
				local max: word `j' of `tcuts' 
				label var `var' "<`max'"
				}
			else if `j' > 1 {
				local k = `j' - 1
				local max: word `j' of `tcuts' 
				local min: word `k' of `tcuts'
				label var `var' "`min'-`max'"
				}
			}
		label var tmissing "Missing"
		}
			
	/**********************************************************************************/
	/* Generate leads and lags														  */			

	if `maxlags' > 0 | `maxleads' > 0 {
	
		sort firedirid distnum
		tsset firedirid distnum

		foreach var of varlist `namelist' {	
			forvalues i = -`maxlags'/`maxleads' {
				local varstub = "`var'"
				local label: variable label `var'
				if `i' < 0 {
					local j = -1*`i'
					gen `varstub'L`j' = L`j'.`var'
					label var `varstub'L`j' "`label'"
					if "`laglabel'" != "" label var `varstub'L`j' `"\hspace{0.5em}\T `j' km within fire perimeter"'
					}
				if `i' >= 0 {
					gen `varstub'F`i' = F`i'.`var'
					label var `varstub'F`i' "`label'"
					if `i' == 0 & "`laglabel'" != "" label var `varstub'F`i' `"\hspace{0.5em}\T At fire perimeter"'
					else if "`laglabel'" != "" label var `varstub'F`i' `"\hspace{0.5em}\T `i' km outside fire perimeter"'
					}
				}
			}
			
		/*Put t-bins in order*/
		if "`tcuts'" != "" {
			forvalues i = -`maxlags'/`maxleads' {
				if `i' >= 0 local stub = "F`i'"
				if `i' < 0 {
					local j = -1*`i'
					local stub = "L`j'"
					}
					
				forvalues k = 3/`q' {
					if `k' > 1 {
						local v = `k' - 1
						order t`k'`stub', after(t`v'`stub')
						}
					}
				}
			}
		}
		
	/**************************************************************************************/
	/* Drop observations after fire has gone out										*/
		
	
	if "`dropout'" == "" | "`dropout'" == "dropout" {
		drop if distnum > duration + 1
		}
	else if "`dropout'" == "nodropout" {
		gen fireout = (distnum > duration + 1)
		}
					
	/**************************************************************************************/
	/* Specify which variables to keep			  										  */
		
	local keeplist 
	foreach var of varlist `namelist' {
		local keeplist `keeplist' `var'*
		}
		
	keep firedirid ignitionid firenum directionnum distnum y duration year pctmissing complexfire fire_size toa `keeplist' fid has_assessor sectionangle mean_windspd mean_windangle wind_diff

	}
	
end
